//
//  StatusOriginalView.swift
//  新浪微博
//
//  Created by seteny on 16/7/14.
//  Copyright © 2016年 seteny. All rights reserved.
//

import UIKit
import SDWebImage


//  原创微博视图
class StatusOriginalView: UIView {

  
    // 持有对应显示数据的viewModel
    var statusViewModel: StatusViewModel? {
        didSet {
            // 判断url 是否为 nil
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                headImageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            timeLabel.text = "刚刚"
            sourceLabel.text = "来着星星的你"
            // 设置微博内容
            contentLabel.text = statusViewModel?.status?.text
            
        }
    }
    
    
    // MARK: -- 懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.grayColor(), fontSize: 15)
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    // 微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        return label
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加视图 设置约束
    private func setupUI() {
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        
        //原创微博的底部约束 = 微博内容的底部约束 + HomeCellMargin
        //很重要的约束
        self.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentLabel).offset(HomeCellMargin)
        }
        
        
    }
    
}
